﻿using Microsoft.Extensions.Logging;
using SqlSugar;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WMS.Model;
using WMS.Repository.IRepository;
using static System.Net.Mime.MediaTypeNames;

namespace WMS.Repository
{
    public class MenuRepository : BaseRepository<MenuModel>, IMenuRepository
    {
        public MenuRepository(ISqlSugarClient db, ILogger<MenuRepository> logger) : base(db, logger)
        {
        }

        /// <summary>
        /// 获取所有菜单（扁平列表）
        /// </summary>
        public async Task<List<MenuModel>> GetAllMenusAsync()
        {
            try
            {
                return await _db.Queryable<MenuModel>().OrderBy(m => m.SortIndex).ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有菜单失败");
                throw;
            }
        }

        /// <summary>
        /// 获取一级菜单
        /// </summary>
        public async Task<List<MenuModel>> GetOneMenuList()
        {
            try
            {
                return await _db.Queryable<MenuModel>()
                               .Where(m => m.ParentMenuId == null && m.MenuType == 1)
                               .ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取一级菜单失败");
                throw;
            }
        }

        /// <summary>
        /// 获取单个菜单
        /// </summary>
        public async Task<MenuModel> GetMenuById(long id)
        {
            try
            {
                return await _db.Queryable<MenuModel>().Where(m => m.Id == id).FirstAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取菜单失败 - 菜单ID: {id}");
                throw;
            }
        }

        /// <summary>
        /// 获取全部菜单
        /// </summary>
        public async Task<List<MenuModel>> GetQueryableList()
        {
            try
            {
                return await _db.Queryable<MenuModel>()
                               .Where(m => m.MenuType == 1 || m.MenuType == 2)
                               .OrderBy(m => m.SortIndex)
                               .ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取菜单列表失败");
                throw;
            }
        }

        /// <summary>
        /// 动态路由查询
        /// </summary>
        public async Task<List<MenuModel>> GetDynamicRouterListAsync(Expression<Func<MenuModel, bool>> whereExpression)
        {
            try
            {
                return await _db.Queryable<MenuModel>()
                               .Where(whereExpression)
                               .ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "动态路由查询失败");
                throw;
            }
        }
    }
}